package com.cmcc.xdr.util;

import java.nio.ByteBuffer;

import com.cmcc.xdr.message.SingleSigAuthenMsg;
import com.cmcc.xdr.message.SingleSigAuthenMsg240;
import com.cmcc.xdr.message.SingleSigGlobalMsg;
import com.cmcc.xdr.message.SingleSigMsg;
import com.cmcc.xdr.message.SingleSigPccMsg;
import com.cmcc.xdr.message.SingleSigPccMsg240;
import com.cmcc.xdr.message.SingleSigSipMsg;
import com.cmcc.xdr.message.SingleSigSipMsg240;
import com.cmcc.xdr.message.SingleSigSrvccMsg;

/**
 * 简单工厂--用于创建不同类型的SingleSigMsg实例
 * @author cf
 *
 */
public class XdrFactory {
    /**
     * 通过类型获取SingleSigMsg实例对象
     * @param type 咖啡类型
     * @return
     */
    public static SingleSigMsg getInstance(String tableName,ByteBuffer buffer,SingleSigGlobalMsg globalMsg,String xdrVersion){
        if("xdr_sipcall".equals(tableName)){
         	if("220".equals(xdrVersion)){
         		SingleSigSipMsg sss = new SingleSigSipMsg(buffer,globalMsg);
         		if(sss.getSipcall().getInterFace() != null || sss.getSipreg().getInterFace() != null||sss.getSipSrvcc().getInterFace() != null){//不能同时等于空，否则会报错
					int interfaceStr = 0;
					if(sss.getSipcall().getInterFace()!=null){
						interfaceStr =sss.getSipcall().getInterFace();
					}else if(sss.getSipreg().getInterFace() != null){
						interfaceStr =sss.getSipreg().getInterFace();
					}else{
						interfaceStr =sss.getSipSrvcc().getInterFace();
					}
					sss.getSSGMsg().setInterfaceType(interfaceStr);
				}
         		return sss;

        	}else{
        		SingleSigSipMsg240 sss = new SingleSigSipMsg240(buffer,globalMsg);
         		if(sss.getSipcall().getInterFace() != null || sss.getSipreg().getInterFace() != null||sss.getSipSrvcc().getInterFace() != null){//不能同时等于空，否则会报错
					int interfaceStr = 0;
					if(sss.getSipcall().getInterFace()!=null){
						interfaceStr =sss.getSipcall().getInterFace();
					}else if(sss.getSipreg().getInterFace() != null){
						interfaceStr =sss.getSipreg().getInterFace();
					}else{
						interfaceStr =sss.getSipSrvcc().getInterFace();
					}
					sss.getSSGMsg().setInterfaceType(interfaceStr);
				}
         		return sss;
        	}
        }else if("xdr_pcc".equals(tableName)){
        	if("220".equals(xdrVersion)){
        		SingleSigPccMsg  pcc = new SingleSigPccMsg(buffer,globalMsg);
        		pcc.getSSGMsg().setInterfaceType(pcc.getPcc().getInterFace());
         		return pcc;
        	}else{
        		SingleSigPccMsg240 pcc = new SingleSigPccMsg240(buffer,globalMsg);
        		pcc.getSSGMsg().setInterfaceType(pcc.getPcc().getInterFace());
        		return pcc;
        	}
        } else if("xdr_srvcc".equals(tableName)){
	        	SingleSigSrvccMsg sv = new SingleSigSrvccMsg(buffer,globalMsg);
				sv.getSSGMsg().setInterfaceType(sv.getSrvcc().getInterFace());
        	return sv;
        } else if("xdr_authen".equals(tableName)){
             	if("220".equals(xdrVersion)){
             		 SingleSigAuthenMsg  authen = new SingleSigAuthenMsg(buffer,globalMsg);
              		authen.getSSGMsg().setInterfaceType(authen.getAuthen().getInterFace());
              		return authen;

             	}else{
             		SingleSigAuthenMsg240 authen = new SingleSigAuthenMsg240(buffer,globalMsg);
             		authen.getSSGMsg().setInterfaceType(authen.getAuthen().getInterFace());
             		return authen;
             	}
        }else{
            throw new RuntimeException("table["+tableName+"]类型不可识别，没有匹配到可实例化的对象！");
        }
		
    }

}